import React, { Component } from "react";
import { Route, Redirect } from "react-router-dom";
import { isAuth } from "../../utils/cacheUtils";
class AuthRoute extends Component {
  render() {
    const { component: Component, ...rest } = this.props;
    return (
      <Route
        {...rest}
        render={(props) => {
          // 判断是否登录
          const isLogin = isAuth();

          // 如果登录
          if (isLogin) {
            return <Component {...props}></Component>;
            // 如果没有登录,则重定向
          } else {
            return (
              <Redirect
                // to={{
                //   pathname: "/loginOrRegister",
                //   state: {
                //     from: props.location.pathname,
                //   },
                // }}
                to={{
                  pathname: "/loginOrRegister",
                  state: { from: props.location },
                }}
              ></Redirect>
            );
          }
        }}
      ></Route>
    );
  }
}

export default AuthRoute;
